import { z } from "zod"; import { guarded, json, body } from "@/lib/api"; import { sql, vec } from "@/lib/db"; import { openaiEmbed } from "@/lib/ai/openai"; export const PATCH = guarded<{ id: string }>(async (req, { params }) => { const b = await body(req, z.object({ content: z.string().min(3).max(2000).optional(), category: z.string().max(30).optional(), status: z.enum(["candidate", "active", "disabled"]).optional(), projectId: z.string().nullable().optional() })); if (b.content) { let emb: number[] | null = null; try { emb = (await openaiEmbed([b.content])).vectors[0]; } catch { emb = null; } await sql`UPDATE memories SET content = ${b.content}, embedding = ${emb ? sql`${vec(emb)}::vector` : null} WHERE id = ${params.id}`; } await sql`UPDATE memories SET category = COALESCE(${b.category ?? null}, category), status = COALESCE(${b.status ?? null}, status), project_id = CASE WHEN ${b.projectId !== undefined} THEN ${b.projectId ?? null} ELSE project_id END WHERE id = ${params.id}`; return json({ ok: true }); }); export const DELETE = guarded<{ id: string }>(async (_req, { params }) => { await sql`DELETE FROM memories WHERE id = ${params.id}`; return json({ ok: true }); });